package com.ty.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.mapper.FettersMapper;
import com.ty.pojo.Fetters;
import com.ty.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class FettersServiceImpl implements FettersService{
    @Autowired
    private FettersMapper fettersMapper;

    @Override
    public PageResult getFettersList(PageResult pageResult, Fetters fetters) {
        IPage<Fetters> iPage = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<Fetters> queryWrapper = new QueryWrapper<>();
        boolean getName = StringUtils.hasLength(fetters.getFettersName());
        queryWrapper.and(Wrapper -> Wrapper.like(getName, "fetters_name", fetters.getFettersName())
                .eq("state", 1));
        iPage = fettersMapper.selectPage(iPage, queryWrapper);
        List<Fetters> records = iPage.getRecords();
        long total = iPage.getTotal();
        return pageResult.setTotal(total).setRows(records);
    }

    @Override
    public void insertFetters(Fetters fetters) {
        fetters.setState(1);
        fettersMapper.insert(fetters);
    }

    @Override
    public void updateFetters(Fetters fetters) {
        fettersMapper.updateById(fetters);
    }

    @Override
    public void updateState(Integer id) {
        QueryWrapper<Fetters> queryWrapper = new QueryWrapper<>();
        Fetters fetters = new Fetters().setState(0);
        queryWrapper.eq("id", id);
        fettersMapper.update(fetters, queryWrapper);
    }
}
